// index.js
import map from "lodash.map";
import { getPageList } from "../../api/order";
Page({
  data: {
    leftMenu: wx.getMenuButtonBoundingClientRect(),
    topHeight: 0,
    searchValue: "",
    visible: false,
    date: new Date().getTime(),
    filterValue: "",
    pageNo: 1,
    pageSize: 10,
    noMoreData: false,
    dataList: [],
  },

  async onShow() {
    let that = this;
    wx.createSelectorQuery()
      .select("#top-container")
      .boundingClientRect(function (rect) {
        that.setData({
          topHeight: rect.height,
        });
      })
      .exec();

    await this.getAllData();
  },
  goDetail(e) {
    wx.navigateTo({
      url: `/pages/data-detail/data-detail?id=${e.detail.id}`,
    });
  },
  onIconTap() {
    wx.navigateTo({
      url: "/pages/add-data/add-data",
    });
  },
  showPicker() {
    this.setData({
      visible: true,
    });
  },

  onTimeCancel() {
    this.setData({
      visible: false,
    });
  },
  async onTimeConfirm(e) {
    this.setData({
      filterValue: e.detail.value,
      visible: false,
    });
    await this.getAllData();
  },
  loadMore() {
    if (!this.data.noMoreData) {
      const pageNo = this.data.pageNo + 1;
      this.getAllData(pageNo * this.data.pageSize);
    }
  },
  async getAllData(pageSize) {
    const [res] = await getPageList({
      page: 1,
      limit: pageSize || 10,
      eventDate: this.data.filterValue,
    });
    let noMoreData = false;
    console.log({ res, pageSize });
    if (res?.data?.total < (pageSize || 10)) {
      console.log({ noMoreData });
      noMoreData = true;
    }
    this.setData({
      noMoreData,
      pageNo: this.data.pageNo + 1,
      dataList: map(res?.data, (item) => {
        return {
          ...item,
        };
      }),
    });
  },
});
